"use strict";
cc._RF.push(module, 'ddea7AZZvtCdqRon/6zq/Qr', 'peopleArcheryRouGePage');
// scripts/Game/Page/peopleArcheryRouGePage.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
var gameEngine_1 = require("../../engines/gameEngine");
var CsvFileKind_1 = require("../../engines/libs/CsvFileKind");
var data_control_1 = require("../../engines/libs/data_control");
var PageBase_1 = require("../Base/PageBase");
var MainRoot_1 = require("../MainRoot");
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var peopleArcheryRouGePage = /** @class */ (function (_super) {
    __extends(peopleArcheryRouGePage, _super);
    function peopleArcheryRouGePage() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.item = null;
        _this.itemContent = null;
        _this.canSyncSpf = null;
        _this.noSyncSpf = null;
        _this.datas = [];
        _this.ariseIds = [];
        _this.oldAriseIds = [];
        _this.maxCount = 3;
        _this.level = null;
        _this.selectId = "";
        return _this;
    }
    peopleArcheryRouGePage.prototype.OnPageOpen = function (prams) {
        return __awaiter(this, void 0, Promise, function () {
            var ids, ariseDataAry, _i, ids_1, id, data;
            return __generator(this, function (_a) {
                _super.prototype.OnPageOpen.call(this, prams);
                ids = gameEngine_1.default.getManagers().csvDataManager.getCsvIds(CsvFileKind_1.CsvFileKind.CsvFile.peopleArcheryMagicWeapon);
                ariseDataAry = [];
                for (_i = 0, ids_1 = ids; _i < ids_1.length; _i++) {
                    id = ids_1[_i];
                    data = gameEngine_1.default.getManagers().csvDataManager.getCsvDataItem(CsvFileKind_1.CsvFileKind.CsvFile.peopleArcheryMagicWeapon, id);
                    if (data["arise_flag"] == "1") {
                        ariseDataAry.push(data);
                    }
                }
                this.datas = ariseDataAry;
                return [2 /*return*/];
            });
        });
    };
    peopleArcheryRouGePage.prototype.bindLevel = function (level) {
        this.level = level;
        this.loadPage();
        this.level.gamePauseFlag = true;
    };
    peopleArcheryRouGePage.prototype.loadPage = function () {
        var _this = this;
        var needCreateCount = this.maxCount - this.itemContent.childrenCount;
        if (needCreateCount > 0) {
            for (var i = 0; i < needCreateCount; i++) {
                var item = cc.instantiate(this.item);
                item.active = true;
                item.setParent(this.itemContent);
                item.on('touchend', this.onSelectItem, this);
            }
        }
        var ariseIds = this.oldAriseIds.concat();
        this.ariseIds = [];
        this.itemContent.children.forEach(function (n) {
            var randData = data_control_1.default.getRandItemOfArray(_this.datas);
            var randId = randData["id"];
            while (ariseIds.includes(randId)) {
                randData = data_control_1.default.getRandItemOfArray(_this.datas);
                randId = randData["id"];
            }
            ariseIds.push(randId);
            _this.ariseIds.push(randId);
            var id = randId;
            _this.refrenshItem(n, randData);
            var sp = n.getChildByName("icon").getComponent(cc.Sprite);
            var syncLb = n.getChildByName("lbSynsic");
            var lbParam = n.getChildByName("lbParam").getComponent(cc.Label);
            // 加载icon
            var iconPath = randData["icon_path"];
            MainRoot_1.default.LevelSystem.getOneTypeBundle(_this.level.getLevelInfo().levelType).load("selectWeapon/" + iconPath, cc.SpriteFrame, function (err, spf) {
                if (err) {
                    console.log("加载道具icon失败", err);
                    return;
                }
                sp.spriteFrame = spf;
            });
            var roleWeapons = _this.level.role.getWeapons();
            var syncFlag = false;
            for (var _i = 0, roleWeapons_1 = roleWeapons; _i < roleWeapons_1.length; _i++) {
                var weaponId = roleWeapons_1[_i];
                var data = gameEngine_1.default.getManagers().csvDataManager.getCsvDataItem(CsvFileKind_1.CsvFileKind.CsvFile.peopleArcheryMagicWeapon, weaponId);
                var can_synthesis_id = data["can_synthesis_id"];
                if (can_synthesis_id == id) {
                    syncFlag = true;
                }
            }
            var bgSp = n.getChildByName("bg").getComponent(cc.Sprite);
            syncFlag = syncFlag && !_this.level.role.synthesisFlag;
            if (syncFlag) {
                bgSp.spriteFrame = _this.canSyncSpf;
            }
            else {
                bgSp.spriteFrame = _this.noSyncSpf;
            }
            syncLb.active = syncFlag;
            var param = randData["param"];
            lbParam.string = "";
            var pAry = param.split("&");
            var typeAry = [];
            var paramAry = [];
            pAry.forEach(function (p) {
                var strAry = p.split("|");
                typeAry.push(strAry["0"]);
                paramAry.push(Number(strAry["1"]));
            });
            for (var i = 0; i < pAry.length; i++) {
                var data = { param_type: typeAry[i], option_param: paramAry[i] };
                var chat = gameEngine_1.default.getManagers().peopleArcheryManager.getOptionText(data);
                lbParam.string += chat + (i == 0 ? "\n" : "");
            }
        });
        this.oldAriseIds = this.ariseIds.concat();
    };
    peopleArcheryRouGePage.prototype.refrenshItem = function (item, data) {
        var name = data["name"];
        var lbName = item.getChildByName("lbName").getComponent(cc.Label);
        lbName.string = name;
        return;
        var iconPath = data["icon_path"];
        MainRoot_1.default.LevelSystem.getOneTypeBundle(this.level.getLevelInfo().levelType).load("");
    };
    peopleArcheryRouGePage.prototype.onSelectItem = function (e) {
        var node = e.target;
        var sliblingIndex = node.getSiblingIndex();
        var id = this.ariseIds[sliblingIndex];
        this.selectId = id;
        this.allNodeNotSelect();
        this.onBtnComit();
        // 将节点设置为选中状态
    };
    peopleArcheryRouGePage.prototype.allNodeNotSelect = function () {
        // 将所有节点设置为未选中状态
    };
    peopleArcheryRouGePage.prototype.onBtnComit = function () {
        if (!this.selectId) {
            gameEngine_1.default.getModules().uiControlModule.showMessageBox("请先选择法宝");
            return;
        }
        this.level.roleAddWeapon(this.selectId);
        this.level.gamePauseFlag = false;
        this.level.role.showTitle("2");
        this.ClosePage();
    };
    peopleArcheryRouGePage.prototype.onBtnRefrensh = function () {
        var _this = this;
        if (gameEngine_1.default.getServices().userSrv.getCoins() > 0) {
            // 有广告币
            this.loadPage();
            gameEngine_1.default.getServices().userSrv.setCoins(-1, true);
        }
        else {
            gameEngine_1.default.getServices().miniGameSrv.getAppApiInstance().loadAndShowVideoAd(function () {
                _this.loadPage();
            }, function () { }, null, "箭箭箭看广告刷新法宝");
        }
    };
    peopleArcheryRouGePage.prototype.update = function (dt) {
        this.level.gamePauseFlag = true;
    };
    peopleArcheryRouGePage.prototype.onDestroy = function () {
        this.level.gamePauseFlag = false;
    };
    __decorate([
        property({ type: cc.Node, tooltip: "item节点" })
    ], peopleArcheryRouGePage.prototype, "item", void 0);
    __decorate([
        property({ type: cc.Node, tooltip: "itemContent" })
    ], peopleArcheryRouGePage.prototype, "itemContent", void 0);
    __decorate([
        property({ type: cc.SpriteFrame, tooltip: "有可合成的显示图片" })
    ], peopleArcheryRouGePage.prototype, "canSyncSpf", void 0);
    __decorate([
        property({ type: cc.SpriteFrame, tooltip: "没有可合成的显示图片" })
    ], peopleArcheryRouGePage.prototype, "noSyncSpf", void 0);
    peopleArcheryRouGePage = __decorate([
        ccclass
    ], peopleArcheryRouGePage);
    return peopleArcheryRouGePage;
}(PageBase_1.default));
exports.default = peopleArcheryRouGePage;

cc._RF.pop();